suite("document", function () {
    "use strict";

    var doc = require("./document");
    var assert = require("assert");

    test("don't create document node without valid id", function () {
        try {
            new doc.Block("some", 0);
            assert(false);
        } catch (error) {
            assert(error.message.indexOf("invalid id") >= 0);
        }

        try {
            new doc.Block(-42, 0);
            assert(false);
        } catch (error) {
            assert(error.message.indexOf("invalid id") >= 0);
        }
    });

    test("don't create document node without valid version", function () {
        try {
            new doc.Block(0, "some");
            assert(false);
        } catch (error) {
            assert(error.message.indexOf("invalid version") >= 0);
        }

        try {
            new doc.Block(0, -42);
            assert(false);
        } catch (error) {
            assert(error.message.indexOf("invalid version") >= 0);
        }
    });

    test("document node doesn't allow changing id after created", function () {
        try {
            var node = new doc.Block(0, 0);
            node.id = 1;
            assert(false);
        } catch (error) {
            assert(error.message.indexOf("id cannot be changed") >= 0);
        }
    });

    test("document node version doesn't get inced on version change", function () {
        var node = new doc.Block(0, 0);
        node.version = 1;
        assert(node.version === 1);
    });

    test("document node version gets inced on property change", function () {
        var block = new doc.Block(0, 0);
        block.position = {x: 1, y: 2};
        assert(block.version > 0);
    });

    test("text block is block", function () {
        var textBlock = new doc.TextBlock(0, 0);
        assert(textBlock instanceof doc.Block);
    });

    test("image block is block", function () {
        var imageBlock = new doc.ImageBlock(0, 0);
        assert(imageBlock instanceof doc.Block);
    });

    test("layout doesn't allow circular templates", function () {
        var template0 = new doc.Layout(0, 0);
        var template1 = new doc.Layout(1, 0);
        template1.template = template0;
        var template2 = new doc.Layout(2, 0);
        template2.template = template1;

        try {
            template0.template = template2;
            assert(false);
        } catch (error) {
            assert(error.message.indexOf("circular templates not allowed") >= 0);
        }
    });

    test("tells recursively if layout is template of another layout", function () {
        var template0 = new doc.Layout(0, 0);
        var template1 = new doc.Layout(1, 0);
        template1.template = template0;
        var template2 = new doc.Layout(2, 0);
        template2.template = template1;

        assert(doc.isTemplateOf(template0, template2));
    });
});
